﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeostatisticalTool.Classes;

namespace GeostatisticalTool.Forms
{
    public partial class TheoreticalModelControl : UserControl
    {

        private TheoreticalModel theo;

        public TheoreticalModel TheoreticalModel
        {
            set
            {
                this.theo = value;
                FillModelForm();
            }
            get { return theo; }
        }

        public event EventHandler ChangedAnisotropyParameter;
        protected virtual void OnChangedAnisotropyParameter(EventArgs e)
        {
            if (ChangedAnisotropyParameter != null)
                ChangedAnisotropyParameter(this, e);
        }


        private IKrigingModel modTemp;
        public TheoreticalModelControl()
        {
            InitializeComponent();
        }




        public void Update1()
        {
            CheckChangesModel();
            this.theo.KrigingModel = modTemp;
        }

        public void Recalculate()
        {
            CheckChangesModel();
            this.theo.FitValues(uxCalSill.Checked, uxCalMajorRange.Checked, uxCalNugget.Checked);
            this.theo.GetFunction(modTemp.ModelType, modTemp.C1, modTemp.Range, modTemp.C0);
            FillModelForm();

        }

        public IKrigingModel KrigingModel
        {
            set
            {
                if (this.theo != null)
                    this.theo.KrigingModel = value;

            }
            get
            {
                if (this.theo == null)
                    return null;

                return this.theo.KrigingModel;
            }
        }



        public String MajorRange
        {
            set { this.uxRangeMajor.Text = value; }
            get { return uxRangeMajor.Text; }

        }

        public void SetModel(Model value)
        {
            int j = 0;
            foreach (Model i in Enum.GetValues(typeof(Model)))
            {
                if (value == i)
                    uxModels.SelectedIndex = j;
                j++;
            }
        }

        public Model GetModel()
        {
            int j = 0;
            foreach (Model i in Enum.GetValues(typeof(Model)))
            {
                if (uxModels.SelectedIndex == j)
                {
                    return i;
                }
                j++;
            }
            return Model.Circular;
        }

        public String MinorRange
        {
            set { uxRangeMinor.Text = value; }
            get { return uxRangeMinor.Text; }

        }

        public String Nugget
        {
            set { uxNugget.Text = value; }
            get { return uxNugget.Text; }

        }

        public String Sill
        {
            set { uxPartialSill.Text = value; }
            get { return uxPartialSill.Text; }

        }

        public String Azimuth
        {
            set { uxAzimuth.Text = value; }
            get { return uxAzimuth.Text; }

        }

        public bool Anysotropy
        {
            set
            {
                if (value)
                {
                    uxAzimuth.Visible = true;
                    uxRangeMinor.Visible = true;
                    label17.Visible = true;
                    label18.Visible = true;
                    uxCalAzimuth.Visible = true;
                    //uxCalMajorRange.Visible = true;
                    uxCalMinorRange.Visible = true;
                }
                else
                {
                    uxAzimuth.Visible = false;
                    uxRangeMinor.Visible = false;
                    label17.Visible = false;
                    label18.Visible = false;
                    uxCalAzimuth.Visible = false;
                    //uxCalMajorRange.Visible = false;
                    uxCalMinorRange.Visible = false;
                }
            }

            get
            {
                return uxAnositopy.Checked;
            }
        }


        private void FillModelForm()
        {
         if(this.theo == null)
                return;


            this.SetModel(this.theo.KrigingModel.ModelType);
            MajorRange = this.theo.KrigingModel.Range.ToString();
            Nugget = this.theo.KrigingModel.C0.ToString();
            Sill = this.theo.KrigingModel.C1.ToString();
            modTemp = this.theo.KrigingModel;
        }

        private void CheckChangesModel()
        {
            if (
                Validator.IsDouble(uxRangeMajor) &&
                Validator.IsDouble(uxPartialSill) &&
                Validator.IsDouble(uxNugget))
            {


                this.modTemp.Range = Convert.ToDouble(uxRangeMajor.Text);
                this.modTemp.C1 = Convert.ToDouble(uxPartialSill.Text);
                this.modTemp.C0 = Convert.ToDouble(uxNugget.Text);
            }
        }

        private void uxModels_SelectedIndexChanged(object sender, EventArgs e)
        {

            int j = 0;
            foreach (Model i in Enum.GetValues(typeof(Model)))
            {
                if (uxModels.SelectedIndex == j)
                {
                    modTemp = TheoreticalModel.SelectModel(i);
                    if (uxRangeMajor.Text != "" && uxPartialSill.Text != "" && uxNugget.Text != "")
                    {
                        modTemp.Range = Convert.ToDouble(uxRangeMajor.Text);
                        modTemp.C1 = Convert.ToDouble(uxPartialSill.Text);
                        modTemp.C0 = Convert.ToDouble(uxNugget.Text);
                    }
                }
                j++;
            }

        }

        private void uxRecalculate_Click(object sender, EventArgs e)
        {
            Recalculate();
        }

        private void TheoreticalModelControl_Load(object sender, EventArgs e)
        {

        }

        private void uxAnositopy_CheckedChanged(object sender, EventArgs e)
        {
            Anysotropy = uxAnositopy.Checked;


        }

        private void uxCalSill_CheckedChanged(object sender, EventArgs e)
        {
            uxPartialSill.Enabled = !uxCalSill.Checked;

        }

        private void uxCalNugget_CheckedChanged(object sender, EventArgs e)
        {

            uxNugget.Enabled = !uxCalNugget.Checked;

        }

        private void uxCalMajorRange_CheckedChanged(object sender, EventArgs e)
        {
            uxRangeMinor.Enabled = !uxCalMajorRange.Checked;
        }

        private void uxCalMinorRange_CheckedChanged(object sender, EventArgs e)
        {
            uxRangeMinor.Enabled = !uxCalMinorRange.Checked;
        }

        private void uxCalAzimuth_CheckedChanged(object sender, EventArgs e)
        {
            uxAzimuth.Enabled = !uxCalAzimuth.Checked;

        }

        private void uxAnositopy_CheckedChanged_1(object sender, EventArgs e)
        {
            Anysotropy = uxAnositopy.Checked;

            OnChangedAnisotropyParameter(EventArgs.Empty);
        }

        private void uxApply_Click(object sender, EventArgs e)
        {
            Update1();
            OnChangedAnisotropyParameter(EventArgs.Empty);
        }

        private void uxCalMajorRange_CheckedChanged_1(object sender, EventArgs e)
        {
            uxRangeMajor.Enabled = !uxCalMajorRange.Checked;
        }

    }
}
